package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.University;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IUniversityService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("university")
public class UniversityController {
    @Autowired
    private IUniversityService universityService;

    @RequiresPermissions(value = {"university:view", "大客户视图"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "university/university";
    }

    @RequiresPermissions(value = {"university:query", "大客户列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(QueryObject qo) {
        return universityService.query(qo);
    }

    @RequiresPermissions(value = {"university:selectAll", "大客户全选"}, logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<University> selectAll() {
        return universityService.selectAll();
    }

    @RequiresPermissions(value = {"university:saveOrUpdate", "大客户编辑/新增"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(University entity) {
        try {
            if (entity.getId() == null) {
                universityService.insert(entity);
            } else {
                universityService.updateByPrimaryKey(entity);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }


    @RequiresPermissions(value = {"university:delete", "大用户删除"}, logical = Logical.OR)
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            universityService.deleteByPrimaryKey(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

    @RequiresPermissions(value={"university:changState","部门状态"},logical = Logical.OR)
    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id) {
        try {
            universityService.changState(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }

}
